package com.liaoyifan.core.converter.handler;

import static com.liaoyifan.core.converter.annotation.MaskConversion.*;

@SuppressWarnings("unused")
public class MaskHandler {

    @MaskEmail
    public static String maskEmail(String email) {
        if (email == null || !email.contains("@")) return email;

        String[] parts = email.split("@", 2);
        String local = parts[0];
        String domain = parts[1];

        if (local.length() <= 2) {
            return "***@" + domain;
        }
        return local.substring(0, 2) + "***@" + domain;
    }

    @MaskPhone
    public static String maskPhone(String phone) {
        if (phone == null) return null;

        // 保留最后4位
        int visibleLength = 4;
        if (phone.length() <= visibleLength) return "****";

        int maskLength = phone.length() - visibleLength;
        return "*".repeat(maskLength) + phone.substring(maskLength);
    }

    @MaskIdCard
    public static String maskIdCard(String idCard) {
        if (idCard == null) return null;

        if (idCard.length() <= 8) return "********";

        int prefix = 4;
        int suffix = 4;
        return idCard.substring(0, prefix)
                + "********"
                + idCard.substring(idCard.length() - suffix);
    }
}
